package com.googlecode.quillen.domain;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;

import java.security.NoSuchAlgorithmException;

/**
 * Created by IntelliJ IDEA.
 * User: greg
 * Date: Dec 2, 2008
 * Time: 9:48:48 PM
 */
public class Chunk
{
    private String key;
    private byte[] data;

    public Chunk(byte[] data) throws NoSuchAlgorithmException
    {
        this(data, 0, data.length);
    }

    public Chunk(byte[] data, int offset, int size) throws NoSuchAlgorithmException
    {
        this.data = new byte[size];
        System.arraycopy(data, offset, this.data, 0, size);
        key = DigestUtils.shaHex(this.data) + "-" + size;
    }

    public String getKey()
    {
        return key;
    }

    public byte[] getData()
    {
        return data;
    }

    public int getSize()
    {
        return data.length;
    }

    public static int getSizeFromKey(String key)
    {
        return Integer.valueOf(StringUtils.substringAfterLast(key, "-"));
    }
}